#include "mytank.h"
int mytank::is_touch()
{int size=39;
    int spe=speed;if(buff2){spe*=2;}
     int move=spe*(maps::mmap[(posy+19)/20][(posx+19)/20]+1);
     int min=move;
    for(int i=0;i<maps::enemynumber;i++)
  {

        switch(direction)
        {
        case 1:
            if(maps::enemyposx[i]>=posx-size&&maps::enemyposx[i]<=posx+size)
            {
               if((posy-move)<=(maps::enemyposy[i]+size)&&maps::enemyposy[i]+39<=posy)
               {
                   if ((posy-(maps::enemyposy[i]+size))<min)
                   {min=(posy-(maps::enemyposy[i]+size));};
               }
            }
            break;
        case 2:
            if(maps::enemyposx[i]>=posx-size&&maps::enemyposx[i]<=posx+size)
            {
               if((posy+size+move)>=maps::enemyposy[i]&&maps::enemyposy[i]>=(posy+size))
               {
                   if( ((maps::enemyposy[i])-posy-size)<min)
                   {min=((maps::enemyposy[i])-posy-size);};
               }
            }
            break;
        case 3:
            if(maps::enemyposy[i]>=posy-size&&maps::enemyposy[i]<=posy+size)
            {
               if((posx-move)<=maps::enemyposx[i]+size&&maps::enemyposx[i]+size<=posx)
               {
                   if ((posx-(maps::enemyposx[i])-size)<min)
                   {
                       min=(posx-(maps::enemyposx[i])-size);
                   };
               }
            }
            break;
        case 4:
            if(maps::enemyposy[i]>=posy-size&&maps::enemyposy[i]<=posy+size)
            {
               if((posx+move+size)>=maps::enemyposx[i]&&maps::enemyposx[i]>=posx+size)
               {
                   if (((maps::enemyposx[i])-size-posx)<min)
                   {
                       min=((maps::enemyposx[i])-size-posx);
                   };
               }
            }
            break;




        }
    }
    return min;
}
void mytank::playerupdate()
{
    if(moveflag)
    {
           switch (direction) {
           case 1:

posy-=base::MIN(is_move(),is_touch());  //1代表上
               break;
           case 2:
              posy+=base::MIN(is_move(),is_touch());  //2代表下
               break;
           case 3:

               posx-=base::MIN(is_move(),is_touch());  //3代表左
               break;
           case 4:
              posx+=base::MIN(is_move(),is_touch()); //4代表右
               break;
    }
    }maps::myy=posy;maps::myx=posx;
};
